package com.jt.sp04orderservice.service;

import com.jt.sp01.entity.Item;
import com.jt.sp01.entity.Order;
import com.jt.sp01.entity.User;
import com.jt.sp01.service.OrderService;
import com.jt.sp04orderservice.feign.ItemClient;
import com.jt.sp04orderservice.feign.UserClient;
import com.jt.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * 订单service
 *
 * @author 郭鲁政
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private UserClient userClient;

    @Override
    public Order getOrder(String orderId) {
        //TODO:远程调用商品,获取商品列表
        JsonResult<List<Item>> items = itemClient.getItems(orderId);
        log.error(items.toString());
        //TODO:远程调用用户,获取用户数据
        JsonResult<User> user = userClient.getUser(8);

        log.info("获取订单,orderId=" + orderId);
        return new Order(orderId, user.getData(), items.getData());

    }

    @Override
    public void addOrder(Order order) {
        log.info("添加订单:    " + order);
        //TODO:远程调用商品,减少库存
        itemClient.decreaseNumber(order.getItems());
        //TODO:远程调用用户,添加积分
        userClient.addScore(order.getUser().getId(), 1000);
        log.info("远程调用成功");
    }

}
